package com.mew.user.gold;

import com.mew.common.dc.db.GoldBuyService;
import com.mew.common.dc.db.LevelConfigService;
import com.mew.common.dc.db.ProfileService;
import com.mew.common.enm.GoldProcessType;
import com.mew.common.entity.config.JewelProduct;
import com.mew.common.entity.config.MoneyProduct;
import com.mew.common.entity.user.Profile;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.gold.GoldBuyMessage;
import com.mew.common.rest.user.BuyRequestMessage;
import com.mew.common.rest.user.UserMoneyResponseMessage;

import com.zebra.common.event.EventBusAsyncCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/buy")
public class GoldBuyController {

    @Autowired
    private ProfileService profileService;

    @Autowired
    private LevelConfigService levelConfigService;
    @Autowired
    private GoldBuyService goldBuyService;

    /**
     * info
     */
    @GetMapping("/info")
    @ResponseBody
    public GoldBuyMessage info(){

        List<MoneyProduct> moneyProduct = goldBuyService.getMoneyProductList();
        List<JewelProduct> jewelProduct = goldBuyService.getJewelProductList();

        GoldBuyMessage message = new GoldBuyMessage();
        message.setMoneyProduct(moneyProduct);
        message.setJewelProduct(jewelProduct);

        return message;
    }

    /**
     * 钻石买金币
     * @return
     */
    @PostMapping("/gold")
    @ResponseBody
    public UserMoneyResponseMessage gold(@RequestParam("uid")long uid, @RequestParam("type")int type){
        Profile profile = profileService.getRedisProfileById(uid);

        JewelProduct jewelProduct = goldBuyService.getJewelProduct(type);

        UserMoneyResponseMessage message = new UserMoneyResponseMessage(profile);

        if (null==jewelProduct){
            return message.buildByResponseCode(ResponseCode.NoProduct);
        }

        long j = jewelProduct.getJewel();

        if (profile.getJewelNumber()<j){
            return message.buildByResponseCode(ResponseCode.JewelNumberNo);
        }

        profileService.doGold(profile,0,-j, 0l,GoldProcessType.Buy.code());
        EventBusAsyncCenter.post(uid); //砖石转换 金币
        return new UserMoneyResponseMessage(profile);
    }

    /**
     * 钻石买金币
     * @return
     */
    @PostMapping("/gold2")
    @ResponseBody
    public UserMoneyResponseMessage gold2(@RequestParam("uid")long uid, @RequestParam("num")long num){
        Profile profile = profileService.getRedisProfileById(uid);


        if (profile.getJewelNumber()<num){
            return new UserMoneyResponseMessage().buildByResponseCode(ResponseCode.JewelNumberNo);
        }
        profileService.doGold(profile, 0, -num, 0l,GoldProcessType.Buy.code());
        EventBusAsyncCenter.post(uid);   //砖石转换 金币
        return new UserMoneyResponseMessage(profile);
    }


    /**
     * 钱买钻石
     * @return
     */
    @PostMapping("/jewelNumber")
    @ResponseBody
    public UserMoneyResponseMessage jewelNumber(BuyRequestMessage req){
        long uid = req.getUid();

        //todo 验证订单


        Profile profile = profileService.getRedisProfileById(uid);

//        MoneyProduct moneyProduct = goldBuyService.getMoneyProduct(req.getProductId());
//
//        if (null==moneyProduct){
//            return new UserMoneyResponseMessage().buildByResponseCode(ResponseCode.NoProduct);
//        }
//
//        long m = moneyProduct.getMoney();
//        long j = moneyProduct.getJewel();
//
//        levelConfigService.vipLevelUp(uid,m);
//
//        profileService.doGold(profile,0l,j, 0l,GoldProcessType.Buy.code());

        return new UserMoneyResponseMessage(profile);
    }




}
